package com.padgett.resourcemanagement.service;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;
import com.padgett.resourcemanagement.data.ClientDAO;

/**
 * Delete service
 * 
 * @author andyleung
 * 
 */
@SuppressWarnings("serial")
public class AjaxDeleteServiceServlet extends HttpServlet{
	private static final Logger logger=Logger.getLogger(AjaxDeleteServiceServlet.class.getName());
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String serviceId=req.getParameter("serviceId");
		Key key=KeyFactory.stringToKey(serviceId);
		
		boolean deleted=ClientDAO.deleteEntity(key);
		JSONObject response=new JSONObject();
		try {
			if(deleted==true){
				response.put("success", deleted);
			}else{
				response.put("success", deleted);
				response.put("error", "Unable to delete service at the moment.");
			}
		} catch (JSONException e) {
			logger.severe(e.getMessage());
		}
		resp.getWriter().write(response.toString());
	}

}
